﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dapper;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.Crm.CommonRepository;
using ZilLion.Service.Crm.ModelBase.EntityBase.Cache;
using ZilLion.Service.Crm.ModelBase.EntityBase.Identity;
using ZilLion.Service.WebInfrastructure.Base;
using ZilLion.Service.WechatService.Domain.WebApiEntity.WechatCardModel;
using ZilLion.Service.WechatService.Domain.WebApiRepository.InterFace;
using Wxmplist = ZilLion.Service.WechatService.Domain.WebApiEntity.MemberCard.Wxmplist;
using AccWxCardAddDetail = ZilLion.Service.Crm.CommonEntity.AccOpr.Web.AccWxCardAddDetail;

namespace ZilLion.Service.WechatService.Domain.WebApiRepository
{
    public class PaymentRepository : CrmSqlServerRespository<AccInfo>, IPaymentRepository
    {
        public PaymentRepository()
        {
        }
        public PaymentRepository(long? accid) : base(accid)
        {
        }

        public PaymentRepository(IDbContext context) : base(context)
        {
        }

        public PaymentRepository(IDbContext context, long? accid) :base(context)
        {
            this.Accid = accid.GetValueOrDefault();
        }


        public WxShowInfo GetAccWxShowInfo()
        {
            WxShowInfo accinfo = new WxShowInfo();
            DynamicParameters p= CreateDynamicParameters(
                new ParamModel(ParamLabel: "accid", Val: Accid)
                 );
            WxShowInfo item =
                ExecuteProcWithTemptable(accinfo, "pr_AccWxShowInfo_Query", "#WxShowInfo", false,
                p).FirstOrDefault();
            //if (item != null) item.Dycardno = ConfContent.EncryptToBase64Format(item.Dycardno, "trfv3_olweixinpay");
            var pitem=ReturnResponseResultInfo<WxShowInfo>(p) ?? item;
            LoadCache(pitem);
            return pitem;
        }

        private void LoadCache(WxShowInfo item)
        {
            var cacheLoader = GloabConfigCacheLoader.CreateGloabConfigCacheLoader(new WechatServiceContext());
            JltyZklbCacheItem jitem = cacheLoader.GetJltyZklbCache();
            var kyzklbt = jitem?.kyzklbts.FirstOrDefault(tr => tr.zklbid == item.accdisid);
            if (kyzklbt != null) item.accdismc = kyzklbt.zklbmc;
        }

        public AccEntitycard CheckBindingCardNo()
        {
            AccEntitycard reCard =new AccEntitycard();
            DynamicParameters p = CreateDynamicParameters(
                         new ParamModel(ParamLabel: "accid", Val: Accid));
            AccEntitycard item = ExecuteProcWithTemptable(reCard, "pr_AccEntitycard_query_new", "#Entitycardno", false, p).FirstOrDefault();
            return ReturnResponseResultInfo<AccEntitycard>(p) ?? item??new AccEntitycard();
        }

        public ResponseResultInfo CartAdd(AccWxCardAddDetail cardAddDetail)
        {
               DynamicParameters p = CreateDynamicParameters(new ParamModel(ParamLabel: "accid", Val: Accid));
            ExecuteProcWithTemptable(cardAddDetail.Ywcore, "pr_AccWxCardAdd_bytel_new", "#ywcore", true, p);
            return ReturnResponseResultInfo<ResponseResultInfo>(p) ?? new ResponseResultInfo();
        }

        public Wxmplist SResultHandle(Wxmplist relResult, string errcode, string errmsg)
        {
            DynamicParameters p = CreateDynamicParameters(
              new ParamModel(ParamLabel: "errcode", Val: errcode),
              new ParamModel(ParamLabel: "errmsg", Val: errmsg)
               );
            Wxmplist item = ExecuteProcWithTemptable(relResult, "pr_wxmpuinfo_resulthandle", "#wxmplist", true, new { errcode, errmsg }).FirstOrDefault();
            return item;
        }

        public AccInfo GetAccInfoquery()
        {
            List<Action<IDbConnection>> paActions = new List<Action<IDbConnection>>();
            AccInfo accinfo = new AccInfo();
            DynamicParameters p = CreateDynamicParameters(
                new ParamModel(ParamLabel: "accid", Val: Accid)
                 );
            AccInfo item =
                ExecuteProcWithTemptable(accinfo, "pr_AccSta_Single_Query_new", "#AccInfo", false,
                p).FirstOrDefault();

            return ReturnResponseResultInfo<AccInfo>(p) ?? item;
        }

        public RelResult GetAccWxmprel(string openappid,string code,string cardid)
        {
            RelResult relResult=new RelResult();
            DynamicParameters p = CreateDynamicParameters(
              new ParamModel(ParamLabel: "accid", Val: Accid),
              new ParamModel(ParamLabel: "openappid", Val: openappid),
              new ParamModel(ParamLabel: "code", Val: code),
              new ParamModel(ParamLabel: "cardid", Val: cardid)
               );
            RelResult item = ExecuteProcWithTemptable(relResult, "pr_AccWxmprel_query_new", "#relresult", false, p).FirstOrDefault();
            return ReturnResponseResultInfo<RelResult>(p) ?? item;
        }

        public IEnumerable<Wxmplist> GetWxmpuinfo(string appidJ)
        {
            Wxmplist relResult = new Wxmplist();
            DynamicParameters p = CreateDynamicParameters(new ParamModel(ParamLabel: "appid", Val: appidJ));
            IEnumerable<Wxmplist> items = ExecuteProcWithTemptable(relResult, "pr_wxmpuinfo_query", "#wxmplist", false, p);
            return items;
        }
      

        //public Wxmplist SaveWxmpuinfo(Wxmplist relResult)
        //{
        //    DynamicParameters p = CreateDynamicParameters();
        //    Wxmplist item = ExecuteProcWithTemptable(relResult, "pr_wxmpuinfo_query", "#wxmplist", true, p).FirstOrDefault();
        //    return item;
        //}

        //public Wxmplist SResultHandle(Wxmplist relResult, string errcode, string errmsg)
        //{
        //    DynamicParameters p = CreateDynamicParameters(
        //      new ParamModel(ParamLabel: "errcode", Val: errcode),
        //      new ParamModel(ParamLabel: "errmsg", Val: errmsg)
        //       );
        //    Wxmplist item = ExecuteProcWithTemptable(relResult, "pr_wxmpuinfo_resulthandle_new", "#wxmplist", true, new { errcode, errmsg }).FirstOrDefault();
        //    return item;
        //}
    }
}
